package warehouse;

import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Database {

    public static Connection con = null;
    public static Statement pst = null;
    public static String user = "root";
    public static String password = "mapa";

    public static void connect() {
        String url = "jdbc:mysql://localhost/siatka";
        try {

            con = DriverManager.getConnection(url, user, password);
            pst = con.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void disconnect() {
        try {
            if (pst != null) {
                pst.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        }
    }

    public double getSpectatorsRateHall(int id) {
        ResultSet rs;
        connect();
        double rate = 0.0;
        try {
            String query;
            query = "SELECT SUM(Liczba_widzow), COUNT(Liczba_widzow) FROM statystyki_hali WHERE Id_Hali=" + id + "; ";
            rs = pst.executeQuery(query);
            rs.next();
            int totalSpectators = rs.getInt(1);
            int numberOfGames = rs.getInt(2);
            if(numberOfGames>0){
            rate = totalSpectators / numberOfGames;
            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return rate;
    }

    public String getTheBestHallSpectatorsRate() {
        ResultSet rs;
        connect();
        String result = "";
        try {
            String query;
            query = "SELECT COUNT(id) FROM hale";
            rs = pst.executeQuery(query);
            rs.next();
            int numberOfHalls = rs.getInt(1);
            double max = 0.0;
            int maxID = 0;
            double temp = 0.0;
            for (int i = 1; i <= numberOfHalls; i++) {
                temp = getSpectatorsRateHall(i);
                if (temp > max) {
                    maxID = i;
                    max = temp;
                }
            }
            result = getHallNameById(maxID);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }
    
    public TreeMap<Integer, Double> getBestHallSpectatorsRate() {
        ResultSet rs;
        
        HashMap<Integer, Double> map = new HashMap<Integer, Double>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<Integer, Double> sorted_map = new TreeMap(bvc);
        
        connect();
        try {
            String query = "SELECT COUNT(Id) FROM hale;";
            rs = pst.executeQuery(query);
            rs.next();
            int numberOfHalls = rs.getInt(1);

            for (int i = 1; i <= numberOfHalls; i++) {
                map.put(i, getSpectatorsRateHall(i));
            }
            sorted_map.putAll(map);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }

        return sorted_map;
    }            
    
    public int getPlayersPointsNumber(int id) {
        ResultSet rs;
        int points=0;
        connect();
        try {
            String query = "SELECT SUM(Ataki_skonczone) FROM statystyki_zawodnikow WHERE Id_Zawodnika=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            int succAttacks = rs.getInt(1);
            query = "SELECT SUM(Bloki_skuteczne) FROM statystyki_zawodnikow WHERE Id_Zawodnika=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            int succBlocks = rs.getInt(1);
            points = succAttacks+succBlocks;
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }        
        return points;
    }

    public TreeMap<Integer, Integer> getTheBestPointPlayers() {
        ResultSet rs;
        
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<Integer, Integer> sorted_map = new TreeMap(bvc);
        
        connect();
        try {
            String query = "SELECT COUNT(Id) FROM zawodnicy;";
            rs = pst.executeQuery(query);
            rs.next();
            int numberOfPlayers = rs.getInt(1);

            for (int i = 1; i <= numberOfPlayers; i++) {
                map.put(i, getPlayersPointsNumber(i));
            }
            sorted_map.putAll(map);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }

        return sorted_map;
    }
    
    public String getHallNameById(int id) {
        ResultSet rs;
        String result = null;
        connect();
        try {
            String query = "SELECT Nazwa from hale WHERE id=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            result = rs.getString(1);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }

    public String getHallCityById(int id) {
        ResultSet rs;
        String result = null;
        connect();
        try {
            String query = "SELECT Miasto  from hale WHERE id=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            result = rs.getString(1);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }    
    
    public int getHallCapacityById(int id) {
        ResultSet rs;
        int result = 0;
        connect();
        try {
            String query = "SELECT Pojemnosc from hale WHERE id=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            result = rs.getInt(1);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }    
    
    public double getPlayersAttacksRate(int id) {
        double rate = 0.0;
        double result;

        ResultSet rs;
        connect();
        try {
            String query = "SELECT SUM(Ataki_skonczone) FROM statystyki_zawodnikow WHERE Id_Zawodnika=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            double succAttacks = rs.getInt(1);
            query = "SELECT SUM(Ataki_nieskuteczne) FROM statystyki_zawodnikow WHERE Id_Zawodnika=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            double failAttacks = rs.getInt(1);
            //System.out.println(succAttacks);
            //System.out.println(failAttacks);
            if(succAttacks+failAttacks>0){
            rate = succAttacks / (succAttacks + failAttacks);
            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return rate;
    }

    public TreeMap<Integer, Double> getBestPlayersAttackRate() {
        ResultSet rs;
        
        HashMap<Integer, Double> map = new HashMap<Integer, Double>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<Integer, Double> sorted_map = new TreeMap(bvc);
        
        connect();
        try {
            String query = "SELECT COUNT(Id) FROM zawodnicy;";
            rs = pst.executeQuery(query);
            rs.next();
            int numberOfPlayers = rs.getInt(1);

            for (int i = 1; i <= numberOfPlayers; i++) {
                map.put(i, getPlayersAttacksRate(i));
            }
            sorted_map.putAll(map);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }

        return sorted_map;
    }

    public double getTeamsBlocksRate(int id) {
        double rate = 0.0;
        double result;

        ResultSet rs;
        connect();
        try {
            String query = "SELECT SUM(Bloki_skuteczne) FROM statystyki_zespolow WHERE Id_Klubu=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            double succAttacks = rs.getInt(1);
            query = "SELECT SUM(Bloki_nieskuteczne) FROM statystyki_zespolow WHERE Id_Klubu=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            double failAttacks = rs.getInt(1);
            //System.out.println(succAttacks);
            //System.out.println(failAttacks);
            if(succAttacks+failAttacks>0){
            rate = succAttacks / (succAttacks + failAttacks);
            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return rate;
    }    

    public TreeMap<Integer, Double> getBestTeamsBlocksRate() {
        ResultSet rs;
        
        HashMap<Integer, Double> map = new HashMap<Integer, Double>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<Integer, Double> sorted_map = new TreeMap(bvc);
        
        connect();
        try {
            String query = "SELECT COUNT(Id) FROM zespoly;";
            rs = pst.executeQuery(query);
            rs.next();
            int numberOfPlayers = rs.getInt(1);

            for (int i = 1; i <= numberOfPlayers; i++) {
                map.put(i, getTeamsBlocksRate(i));
            }
            sorted_map.putAll(map);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }

        return sorted_map;
    }        
    
    public double getPlayersBlocksRate(int id) {
        double rate = 0.0;
        double result;

        ResultSet rs;
        connect();
        try {
            String query = "SELECT SUM(Bloki_skuteczne) FROM statystyki_zawodnikow WHERE Id_Zawodnika=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            double succAttacks = rs.getInt(1);
            query = "SELECT SUM(Bloki_nieskuteczne) FROM statystyki_zawodnikow WHERE Id_Zawodnika=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            double failAttacks = rs.getInt(1);
            //System.out.println(succAttacks);
            //System.out.println(failAttacks);
            if(succAttacks+failAttacks>0){
            rate = succAttacks / (succAttacks + failAttacks);
            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return rate;
    }

    public TreeMap<Integer, Double> getBestPlayersBlocksRate() {
        ResultSet rs;
        
        HashMap<Integer, Double> map = new HashMap<Integer, Double>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<Integer, Double> sorted_map = new TreeMap(bvc);
        
        connect();
        try {
            String query = "SELECT COUNT(Id) FROM zawodnicy;";
            rs = pst.executeQuery(query);
            rs.next();
            int numberOfPlayers = rs.getInt(1);

            for (int i = 1; i <= numberOfPlayers; i++) {
                map.put(i, getPlayersBlocksRate(i));
            }
            sorted_map.putAll(map);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }

        return sorted_map;
    }    
    
    
    public double getPlayersGambitsRate(int id) {
        double rate = 0.0;
        double result;

        ResultSet rs;
        connect();
        try {
            String query = "SELECT SUM(Asy_serwisowe) FROM statystyki_zawodnikow WHERE Id_Zawodnika=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            double succAttacks = rs.getInt(1);
            query = "SELECT SUM(Zagrywki_zepsute) FROM statystyki_zawodnikow WHERE Id_Zawodnika=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            double failAttacks = rs.getInt(1);
            //System.out.println(succAttacks);
            //System.out.println(failAttacks);
            if(succAttacks+failAttacks>0){
            rate = succAttacks / (succAttacks + failAttacks);
            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return rate;
    }

    public TreeMap<Integer, Double> getBestPlayersGambitsRate() {
        ResultSet rs;
        
        HashMap<Integer, Double> map = new HashMap<Integer, Double>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<Integer, Double> sorted_map = new TreeMap(bvc);
        
        connect();
        try {
            String query = "SELECT COUNT(Id) FROM zawodnicy;";
            rs = pst.executeQuery(query);
            rs.next();
            int numberOfPlayers = rs.getInt(1);

            for (int i = 1; i <= numberOfPlayers; i++) {
                map.put(i, getPlayersGambitsRate(i));
            }
            sorted_map.putAll(map);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }

        return sorted_map;
    }

    public double getTeamsGambitsRate(int id) {
        double rate = 0.0;
        double result;

        ResultSet rs;
        connect();
        try {
            String query = "SELECT SUM(Asy_serwisowe) FROM statystyki_zespolow WHERE Id_Klubu=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            double succAttacks = rs.getInt(1);
            query = "SELECT SUM(Zagrywki_zepsute) FROM statystyki_zespolow WHERE Id_Klubu=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            double failAttacks = rs.getInt(1);
            //System.out.println(succAttacks);
            //System.out.println(failAttacks);
            if(succAttacks+failAttacks>0){
            rate = succAttacks / (succAttacks + failAttacks);
            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return rate;
    }    
    
    public TreeMap<Integer, Double> getBestTeamsGambitsRate() {
        ResultSet rs;
        
        HashMap<Integer, Double> map = new HashMap<Integer, Double>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<Integer, Double> sorted_map = new TreeMap(bvc);
        
        connect();
        try {
            String query = "SELECT COUNT(Id) FROM zespoly;";
            rs = pst.executeQuery(query);
            rs.next();
            int numberOfTeams = rs.getInt(1);

            for (int i = 1; i <= numberOfTeams; i++) {
                map.put(i, getTeamsGambitsRate(i));
            }
            sorted_map.putAll(map);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }

        return sorted_map;
    }    
    
    public String getPlayersNameById(int id) {
        ResultSet rs;
        String result = null;
        connect();
        try {
            String query = "SELECT Nazwisko FROM zawodnicy WHERE id=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            result = rs.getString(1);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }
    public String getPlayersFirstNameById(int id) {
        ResultSet rs;
        String result = null;
        connect();
        try {
            String query = "SELECT Imie FROM zawodnicy WHERE id=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            result = rs.getString(1);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }
    public String getPlayersTeamNameById(int id) {
        ResultSet rs;
        String result = null;
        connect();
        try {
            String query = "SELECT Id_Klubu FROM zawodnicy WHERE id=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            result = rs.getString(1);
            query = "SELECT Nazwa FROM zespoly WHERE id=" + result + ";";
            rs = pst.executeQuery(query);
            rs.next();
            result = rs.getString(1);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }
    
    
    public double getTeamsAttackRate(int id) {
        double rate = 0.0;
        double result;

        ResultSet rs;
        connect();
        try {
            String query = "SELECT SUM(Ataki_skonczone) FROM statystyki_zespolow WHERE Id_Klubu=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            double succAttacks = rs.getInt(1);
            query = "SELECT SUM(Ataki_nieskuteczne) FROM statystyki_zespolow WHERE Id_Klubu=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            double failAttacks = rs.getInt(1);
            //System.out.println(succAttacks);
            //System.out.println(failAttacks);
            if(succAttacks+failAttacks>0){
            rate = succAttacks / (succAttacks + failAttacks);
            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return rate;        
    }

    
    public String getTeamsCountryById(int id) {
        ResultSet rs;
        String result = null;
        connect();
        try {
            String query = "SELECT Kraj FROM zespoly WHERE id=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            result = rs.getString(1);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }    
    
    public String getTeamsLeagueById(int id) {
        ResultSet rs;
        String result = null;
        connect();
        try {
            String query = "SELECT Liga FROM zespoly WHERE id=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            result = rs.getString(1);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }    
    
    public int getTeamsPointsById(int id) {
        ResultSet rs;
        int result = 0;
        connect();
        try {
            String query = "SELECT Punkty FROM zespoly WHERE id=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            result = rs.getInt(1);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }    
    
    public String getTeamsNameById(int id) {
        ResultSet rs;
        String result = null;
        connect();
        try {
            String query = "SELECT Nazwa FROM zespoly WHERE id=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            result = rs.getString(1);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }
    
    public TreeMap<Integer, Double> getBestTeamsAttackRate() {
        ResultSet rs;

        HashMap<Integer, Double> map = new HashMap<Integer, Double>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<Integer, Double> sorted_map = new TreeMap(bvc);        
        
        connect();
        try {
            String query = "SELECT COUNT(Id) FROM zespoly;";
            rs = pst.executeQuery(query);
            rs.next();
            int numberOfTeams = rs.getInt(1);            
            
            for (int i = 1; i <= numberOfTeams; i++) {
                map.put(i, getTeamsAttackRate(i));
            }
            sorted_map.putAll(map);            
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        
        return sorted_map;
    }
    
    public double getArbitersRate(int id) {
        ResultSet rs;
        connect();
        double rate = 0.0;
        try {
            String query;
            query = "SELECT SUM(Ocena_obserwatora), COUNT(Data) FROM statystyki_sedziow WHERE Id_Sedziego=" + id + "; ";
            rs = pst.executeQuery(query);
            rs.next();
            double totalGrade = rs.getInt(1);
            double numberOfGames = rs.getInt(2);
            if(numberOfGames>0){
            rate = totalGrade / numberOfGames;
            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return rate;
    }

    
    public TreeMap<Integer, Double> getBestArbitersRate() {
        ResultSet rs;
        
        HashMap<Integer, Double> map = new HashMap<Integer, Double>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<Integer, Double> sorted_map = new TreeMap(bvc);
        
        connect();
        try {
            String query = "SELECT COUNT(Id) FROM sedziowie;";
            rs = pst.executeQuery(query);
            rs.next();
            int numberOfArbiters = rs.getInt(1);

            for (int i = 1; i <= numberOfArbiters; i++) {
                map.put(i, getArbitersRate(i));
            }
            sorted_map.putAll(map);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }

        return sorted_map;
    }    
    
    public String getTheBestArbitersRate() {
        ResultSet rs;
        connect();
        String result = "";
        try {
            String query;
            query = "SELECT COUNT(id) FROM sedziowie";
            rs = pst.executeQuery(query);
            rs.next();
            int numberOfArbiters = rs.getInt(1);
            double max = 0.0;
            int maxID = 0;
            double temp = 0.0;
            for (int i = 1; i <= numberOfArbiters; i++) {
                temp = getArbitersRate(i);
                if (temp > max) {
                    maxID = i;
                    max = temp;
                }
            }
            result = getArbitersNameById(maxID);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }

    public String getArbitersNameById(int id) {
        ResultSet rs;
        String result = null;
        connect();
        try {
            String query = "SELECT Nazwisko from sedziowie WHERE id=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            result = rs.getString(1);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }  
    public String getArbitersFirstNameById(int id) {
        ResultSet rs;
        String result = null;
        connect();
        try {
            String query = "SELECT Imie from sedziowie WHERE id=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            result = rs.getString(1);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }   
    public String getArbitersNationalityById(int id) {
        ResultSet rs;
        String result = null;
        connect();
        try {
            String query = "SELECT Narodowosc from sedziowie WHERE id=" + id + ";";
            rs = pst.executeQuery(query);
            rs.next();
            result = rs.getString(1);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }
    
    
    public int SelectInt(String query) {
        ResultSet rs;
        int result = 0;
        connect();
        try {
            rs = pst.executeQuery(query);
            while (rs.next()) {
                result = rs.getInt(1);
            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }

    public String SelectString(String query) {
        ResultSet rs;
        String result = null;
        connect();
        try {
            rs = pst.executeQuery(query);
            while (rs.next()) {
                result = rs.getString(1);
            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return result;
    }
    
    public Vector<String> executeQuery(String query) {
        int maxI=0;
        ResultSet rs;
        String result = null;
        Vector<String> vector = new Vector();
        connect();
        try {
            rs = pst.executeQuery(query);
            while (rs.next()) {
                try {
                    for (int i=1; i<=4; i++) {
                        result = rs.getString(i);
                        //System.out.println(result);
                        vector.add(result);
                        maxI=i;
                    }
                } catch (SQLException ex) {
                    
                }
            }
        vector.add(Integer.toString(maxI));
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        return vector;
    }

    class ValueComparator implements Comparator {

        Map base;

        public ValueComparator(Map base) {
            this.base = base;
        }

        public int compare(Object a, Object b) {
            if(base.get(b).getClass()==Double.class){
            if ((Double) base.get(a) < (Double) base.get(b)) {
                return 1;
            } else if ((Double) base.get(a) == (Double) base.get(b)) {
                return 0;
            } else {
                return -1;
            }
        } else {
              if ((Integer) base.get(a) < (Integer) base.get(b)) {
                return 1;
            } else if ((Integer) base.get(a) == (Integer) base.get(b)) {
                return 0;
            } else {
                return -1;
            }  
            }
        }
    }
}
